//
// Created by tonystark on 16/4/14.
// Copyright (c) 2016 sewm. All rights reserved.
//

#import "UpLoadData.h"
#import "Api.h"


@implementation UpLoadData {

}

- (void)setFilePath:(NSString *)filePath {
    _filePath = filePath;
//    self.fileName = [filePath pathPonent];
//    self.data = [NSData dataWithContentsOfFile:_filePath];
}

- (void)setFileName:(NSString *)fileName {
    _fileName = fileName;
    self.format = [self formatFromFileName];
}

- (void)setFormat:(NSString *)format {
    _format = format;
    self.mimeType = [self mimeTypeFormFormat:_format];
}

- (NSString *)mimeTypeFormFormat:(NSString *)format {
    NSDictionary *dci = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"fileType.strings"]];
    return dci[format];
}

- (void)upLoadWithCompletion:(void (^)(BOOL success, NSString *errorMessage))completion {
    WeakSelf
    [Api getConnection:^(id body) {
        weakSelf.fileURL = body[@"showPath"];
        completion(YES, nil);
    }             fail:^(NSString *message, NSInteger code) {
        completion(NO, message);
    }         bodyDict:@{@"dir" : self.format} service:@"upload" mutiDatas:@[self]];
}

- (NSString *)formatFromFileName {
    NSMutableArray *type = [NSMutableArray array];
    for (NSInteger i = [_fileName length] - 1; i >= 0; i--) {
        NSString *charStr = [NSString stringWithFormat:@"%c", [_fileName characterAtIndex:i]];
        if ([charStr isEqualToString:@"."]) {
            break;
        }
        [type insertObject:charStr atIndex:0];
    }
    return [type componentsJoinedByString:@""];

}
@end